package jhunter.dao.condition;

import java.util.ArrayList;
import java.util.List;

public class ConditionBuilder implements Condition {

	private List<Condition> conditions = new ArrayList<Condition>();

	private List<String> operations = new ArrayList<String>();

	public ConditionBuilder and(Condition condition) {
		if (conditions.size() == 0) {
			operations.add("");
		} else {
			operations.add("and");
		}
		conditions.add(condition);
		return this;
	}

	public ConditionBuilder or(Condition condition) {
		if (conditions.size() == 0) {
			operations.add("");
		} else {
			operations.add("or");
		}
		conditions.add(condition);
		return this;
	}

	@Override
	public String getWhereSql() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < operations.size(); i++) {
			sb.append(operations.get(i)).append(" ");
			sb.append(conditions.get(i).getWhereSql());
		}
		return sb.toString().trim();
	}

	@Override
	public boolean isEmpty() {
		return conditions.size() == 0;
	}

	@Override
	public List<Object> getParameters() {
		List<Object> list = new ArrayList<Object>();
		for (Condition cd : conditions) {
			list.addAll(cd.getParameters());
		}
		return list;
	}

}
